package cn.iocoder.yudao.module.promotion.service.shortlinkdetail;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.promotion.controller.admin.shortlinkdetail.vo.*;
import cn.iocoder.yudao.module.promotion.convert.shortlinkdetail.ShortLinkDetailConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.shortlinkdetail.ShortLinkDetailDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.shortlinkdetail.ShortLinkDetailMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.SHORT_LINK_DETAIL_NOT_EXISTS;
import static cn.iocoder.yudao.module.promotion.util.PromotionUtils.*;

/**
 * 短链接列 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ShortLinkDetailServiceImpl implements ShortLinkDetailService {

    @Resource
    private ShortLinkDetailMapper shortLinkDetailMapper;

    @Override
    public Long createShortLinkDetail(ShortLinkDetailCreateReqVO createReqVO) {
        // 插入
        ShortLinkDetailDO shortLinkDetail = ShortLinkDetailConvert.INSTANCE.convert(createReqVO);
        shortLinkDetailMapper.insert(shortLinkDetail);
        // 返回
        return shortLinkDetail.getId();
    }

    @Override
    public void updateShortLinkDetail(ShortLinkDetailUpdateReqVO updateReqVO) {
        // 校验存在
        validateShortLinkDetailExists(updateReqVO.getId());
        // 更新
        ShortLinkDetailDO updateObj = ShortLinkDetailConvert.INSTANCE.convert(updateReqVO);
        shortLinkDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteShortLinkDetail(Long id) {
        // 校验存在
        validateShortLinkDetailExists(id);
        // 删除
        shortLinkDetailMapper.deleteById(id);
    }

    @Override
    public void deleteShortLinkDetailList(Long shortLinkId) {
        // 删除
        shortLinkDetailMapper.deleteByShortLinkId(shortLinkId);
    }

    private void validateShortLinkDetailExists(Long id) {
        if (shortLinkDetailMapper.selectById(id) == null) {
            throw exception(SHORT_LINK_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public ShortLinkDetailDO getShortLinkDetail(Long id) {
        return shortLinkDetailMapper.selectById(id);
    }

    @Override
    public List<ShortLinkDetailDO> getShortLinkDetailList(Collection<Long> ids) {
        return shortLinkDetailMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShortLinkDetailDO> getShortLinkDetailPage(ShortLinkDetailPageReqVO pageReqVO) {
        IPage<ShortLinkDetailDO> result = shortLinkDetailMapper.selectListPage(MyBatisUtils.buildPage(pageReqVO),pageReqVO);
        return new PageResult<>(result.getRecords(), result.getTotal());
    }

    @Override
    public List<ShortLinkDetailDO> getShortLinkDetailList(ShortLinkDetailExportReqVO exportReqVO) {
        return shortLinkDetailMapper.selectList(exportReqVO);
    }

    @Override
    public ShortLinkDetailImportResponseVO deliveryShortLink(List<ShortLinkDetailImportExcelVO> deliveryList, Long shortLinkId, String name, String shortLinkDomain) {
        ShortLinkDetailImportResponseVO responseVO = new ShortLinkDetailImportResponseVO();
        if (CollectionUtil.size(deliveryList) == 0) {
            return null;
        }
        // 标题
        deliveryList = CollectionUtil.sub(deliveryList, 0, deliveryList.size());
        Map<String, String> failureOrders = new HashMap<>();
        List<String> successOrders = new ArrayList<>();
        String batchNumber = generateBatchNumber();
        List<ShortLinkDetailDO> shortLinkDetailList = new ArrayList<>();
        deliveryList.forEach(item -> {
                    try {
                        ShortLinkDetailCreateReqVO createReqVO = new ShortLinkDetailCreateReqVO();
                        createReqVO.setShortLinkId(shortLinkId);
                        createReqVO.setName(name);
                        createReqVO.setStatus((byte) 0);
                        createReqVO.setBatchNumber(batchNumber);
                        createReqVO.setPhoneNumber(item.getPhoneNumber());
                        createReqVO.setShortUrl(shortLinkDomain + "/" + generateRandomShortLink());
                        ShortLinkDetailDO shortLinkDetail = ShortLinkDetailConvert.INSTANCE.convert(createReqVO);
                        shortLinkDetailList.add(shortLinkDetail);
                        successOrders.add(item.getPhoneNumber().toString());
                    } catch (Exception e) {
                        failureOrders.put(item.getPhoneNumber().toString(), e.getMessage());
                    }
                }
        );
        shortLinkDetailMapper.insertBatch(shortLinkDetailList);
        responseVO.setFailureOrders(failureOrders);
        responseVO.setUpdateOrders(successOrders);
        return responseVO;
    }

    @Override
    public ShortLinkDetailDO getLongUrl(ShortLinkDetailPageReqVO pageReqVO) {
        LambdaQueryWrapperX<ShortLinkDetailDO> lambdaQueryWrapper = new LambdaQueryWrapperX<>();
        lambdaQueryWrapper.eq(ShortLinkDetailDO::getShortUrl, pageReqVO.getShortUrl());
        return shortLinkDetailMapper.selectOne(lambdaQueryWrapper);
    }

}
